package com.luli.Utils;

import com.luli.entity.poi.ExportExcel;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class EncryptUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(EncryptUtils.class);


    public static void encryAndExportByResponse(HttpServletRequest request,
                                                HttpServletResponse response,
                                                ExportExcel exportExcel, ReportPropsConf reportPropsConf) throws IOException {
        //将文件输出到临时报表目录中
        File file = exportExcel.export(getReportTmpPath(reportPropsConf));
        if (file != null) {
            if ("true".equalsIgnoreCase(reportPropsConf.getFileEncrypted())) {
                //将临时目录中的报表文件复制到加路径，并加密
                String fileDirPath = reportPropsConf.getReportUploadPath() + reportPropsConf.getReportEncryptArchivePath();
                fileDirPath = URLDecoder.decode(fileDirPath, "UTF-8").replace("/", File.separator);
                File dirFile = new File(fileDirPath);
                if (!dirFile.exists() && dirFile.isDirectory()) {
                    dirFile.mkdirs();
                }
                String fileName = file.getName();
                String encFile = fileDirPath + fileName;
                FileUtils.copyFile(file, new File(encFile));
                CryptSDK sdk = new CryptSDK();
                sdk.encryptFile(encFile, encFile);
                //解密
                //        String decFilePath=reportRootPath+reportDecryptArchivePath;
                //        decFilePath = URLDecoder.decode(decFilePath, "UTF-8").replace("/", File.separator);
                //        sdk.decryptFile(encFile, decFilePath+fileName);
                File fileEnc = new File(encFile);
                if (fileEnc.exists()) {
                    FileUtil.output(response, request, fileEnc);
                } else {
                    LOGGER.error("");
                }
            } else {
                FileUtil.output(response, request, file);
            }
        }
    }


    /**
     * @param
     * @return java.lang.String
     * @desc 获取报表临时路径
     * @date wxs 2018-12-06 14:20
     */
    private static String getReportTmpPath(ReportPropsConf reportPropsConf) throws UnsupportedEncodingException {
        String classPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        String webAppPath = new File(classPath).getParentFile().getParent();
        return URLDecoder.decode((webAppPath + reportPropsConf.getReportTempArchivePath()), "UTF-8")
                .replace("/", File.separator);
    }



    public static void encryAndExportByResponseList(HttpServletRequest request,
                                                    HttpServletResponse response,
                                                    File file, ReportPropsConf reportPropsConf) throws IOException {
        //将文件输出到临时报表目录中
        if (file != null) {
            if ("true".equalsIgnoreCase(reportPropsConf.getFileEncrypted())) {
                //将临时目录中的报表文件复制到加路径，并加密
                String fileDirPath = reportPropsConf.getReportUploadPath() + reportPropsConf.getReportEncryptArchivePath();
                fileDirPath = URLDecoder.decode(fileDirPath, "UTF-8").replace("/", File.separator);
                File dirFile = new File(fileDirPath);
                if (!dirFile.exists() && dirFile.isDirectory()) {
                    dirFile.mkdirs();
                }
                String fileName = file.getName();
                String encFile = fileDirPath + fileName;
                FileUtils.copyFile(file, new File(encFile));
                CryptSDK sdk = new CryptSDK();
                sdk.encryptFile(encFile, encFile);
                File fileEnc = new File(encFile);
                if (fileEnc.exists()) {
                    FileUtil.output(response, request, fileEnc);
                } else {
                    LOGGER.error("");
                }
            } else {
                FileUtil.output(response, request, file);
            }
        }
    }


    /**
     * @param
     * @return java.lang.String
     * @desc 获取报表临时路径
     */
    public static String getReportTmpPaths(ReportPropsConf reportPropsConf) throws UnsupportedEncodingException {
        String classPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        if (classPath.startsWith("file:/")) {
            classPath = classPath.substring(5, classPath.length());
        }
        String webAppPath = new File(classPath).getParentFile().getParent();
        if (StringUtils.isNotEmpty(webAppPath)) {
            if (webAppPath.contains(".jar!")) {
                webAppPath = webAppPath.replace(".jar!", "");
            }
        }
        return URLDecoder.decode((webAppPath + reportPropsConf.getReportTempArchivePath()), "UTF-8")
                .replace("/", File.separator);
    }
}
